<?php

namespace App\Http\Controllers\Manage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

use App\Model\Share;
use App\Model\ShareCategory;
use OSS\Core\OssException;
class ShareController extends Controller
{
    /**
     * 分享列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function shareList() {
        $page = Input::get('page');
        $count = Input::get('count');
        $orderBy = Input::get('orderBy');
        $obj = new Share();
        #判断时间排序
        if($orderBy == 1){
            $obj = $obj->orderBy('update_time','ASC');
        }elseif($orderBy == 2){
            $obj = $obj->orderBy('update_time','DESC');
        }else{
            $obj = $obj->orderBy('rank','DESC')->orderBy('update_time','DESC');
        }
        $share = $obj->Paginate(20);
        if(empty($page)){
            $count = $obj->count();
        }
        return view('manage.share.shareList',[
            'share'=>$share,
            'count'=>$count,
            'orderBy'=>$orderBy,
            'active1'=>11,
            'active2'=>1
        ]);
    }

    /**
     * 分享启禁用
     * @return array
     */
    public function shareStatus() {
        $id = Input::get('id');
        $status = Input::get('status');
        if($status == 1) {
            $del = Share::where('id',$id)->update(['status'=>1]);
            $msg = '启用成功！';
        }else{
            $del = Share::where('id',$id)->update(['status'=>0]);
            $msg = '禁用成功！';
        }
        if(!$del){
            return [
                'code'=>101,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }

    /**
     * 分享详情
     * @param $id
     */
    public function details($id) {
        $share = Share::getInfo($id);
        $share['att'] = explode(',',$share['attachment']);

        return view('manage.share.share_details',['share'=>$share,'active1'=>11,'active2'=>1]);
    }

    /**
     * 分类列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function categoryList() {
        $cate = ShareCategory::getAllInfo();
        $count = ShareCategory::count();
        return view('manage.share.categoryList',['cate'=>$cate,'count'=>$count,'active1'=>11,'active2'=>2]);
    }

    /**
     * 分享启禁用
     * @return array
     */
    public function shareCateStatus() {
        $id = Input::get('id');
        $status = Input::get('status');
        if($status == 1) {
            $sta = ShareCategory::where('id',$id)->update(['status'=>1]);
            $msg = '启用成功！';
        }else{
            $sta = ShareCategory::where('id',$id)->update(['status'=>0]);
            $msg = '禁用成功！';
        }
        if(!$sta){
            return [
                'code'=>101,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }

    /**
     * 分类信息
     * @param $id
     */
    public function categoryEdit($id) {
        $cate = ShareCategory::getOneInfo($id);
        return view('manage.share.categoryEdit',['cate'=>$cate,'active1'=>11,'active2'=>2]);
    }

    public function update() {
        #设置oss参数
        $ossClient = $this->oss();
        $id = input::get('id');
        $data['rank'] = input::get('rank');
        $data['name'] = input::get('name');
        $data['icon'] = input::get('path');
        $before = ShareCategory::where('id',$id)->first();

        if(empty($data['name'])) {
            try {
                $ossClient->deleteObject($this->bucket, $data['icon']);
            } catch (OssException $e) {
//                        print $e->getMessage();
            }
            return [
                'code' => 102,
                'msg' => '缺少元素!'
            ];
        }
        $update = ShareCategory::where('id',$id)->update($data);
        if($update) {
            try {
                $ossClient->deleteObject($this->bucket, $before->icon);
            } catch (OssException $e) {
//                        print $e->getMessage();
            }
            return [
                'code' => 100,
                'msg' => '修改成功!'
            ];
        }
        else {
            try {
                $ossClient->deleteObject($this->bucket,$data['icon']);
            } catch (OssException $e) {
//                        print $e->getMessage();
            }
            return [
                'code' => 101,
                'msg' =>'修改失败!'
            ];
        }
    }
}
